package org.example.BQ;

import java.util.concurrent.*;

public class ThreadPool {
    public static void main(String[] args) {
//         ExecutorService threadPool = Executors.newSingleThreadExecutor(); // 单个线程
//         ExecutorService threadPool1 =  Executors.newFixedThreadPool(5); // 创建一个固定的线程池大小
//         ExecutorService threadPool2 =  Executors.newCachedThreadPool(); // 可伸缩的
        ExecutorService threadPool = new ThreadPoolExecutor(
                2,
                5,
                2,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy()   // 队列满了，不会抛出异常，丢掉队列最老的哪个代替他进入队列
        );
        try {
            for (int i = 1; i <= 9; i++) {
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"ok");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }

    }
}
